class CommentsController < ApplicationController
  before_filter :common
  before_filter :authorize, :only => [ :create, :edit, :update ]
  before_filter :authorize_admin, :only => [ :destroy ]

  # GET /projects/project1/issues/1/comments
  # GET /projects/project1/issues/1/comments.xml
  def index
    respond_to do |format|
      # redirect to the appropriate issue because comments are listed there.
      # html only; we assume people know what they're doing.
      format.html { redirect_to issue_url( @project.name, @issue.local_id ) }
      format.xml  { render :xml => Comment.find(:all) }
    end
  end

  # GET /projects/project1/issues/1/comments/1
  # GET /projects/project1/issues/1/comments/1.xml
  def show
    @comment = Comment.find(params[:id])

    respond_to do |format|
      format.html # show.html.erb
      format.xml  { render :xml => @comment }
    end
  end

  # GET /comments/1/edit
  def edit
    @comment = Comment.find(params[:id])
    puts "RUN!"

    respond_to do |format|
      format.html # edit.html.erb
    end
  end

  # POST /projects/project1/issues/1/comments
  # POST /projects/project1/issues/1/comments.xml
  def create
    @comment = Comment.new( params[:comment] )
    @issue.comments << @comment if @issue
    @comment.user = @current_user if @current_user
    @comment.attachments = get_attachments
    @comment.issue_changes = @issue.changes( params[:issue] )

    respond_to do |format|
      if @comment.save and @issue.update_attributes( params[:issue] )
        @comment.reload # get the correct cached association sizes
        flash.now[:notice] = 'Comment was successfully created.'
        send_update_success_email( @issue, @comment )
        format.html { redirect_to( comment_url( @project.name, @issue.local_id, @comment ) ) }
        format.xml  { render :xml => @comment, :status => :created, :location => @comment }
        format.js do
          responds_to_parent do
            render :update do |page|
              page.replace( 'flash',
                            :partial => 'shared/flash',
                            :locals => { :flash => flash } )
              page.replace( 'new_comment',
                            :partial => 'shared/comment',
                            :locals => { :comment => @comment } )
              page.replace( 'details',
                            :partial => 'issues/details',
                            :locals => { :issue => @issue } )
              page.insert_html( :after, dom_id( @comment ), '<div id="new_comment"></div>' )
              page.replace( 'comment_form',
                            :partial => 'shared/comment_form',
                            :locals => {
                              :project => @project,
                              :issue => @issue,
                              :comment => Comment.new
                            } )
            end
          end
        end
      else
        format.html { render :action => 'new' }
        format.xml  { render :xml => @comment.errors, :status => :unprocessable_entity }
        format.js do
          responds_to_parent do
            render :update do |page|
              page.replace( 'flash',
                            :partial => 'shared/flash',
                            :locals => { :flash => flash } )
              page.replace( 'comment_form',
                            :partial => 'shared/comment_form',
                            :locals => {
                              :project => @project,
                              :issue => @issue,
                              :comment => @comment
                            } )
            end
          end
        end
      end
    end
  end

  # PUT /projets/project1/issues/1/comments/1
  # PUT /projets/project1/issues/1/comments/1.xml
  def update
    @comment = Comment.find(params[:id])
    @comment.attachments = get_attachments

    respond_to do |format|
      if @comment.update_attributes(params[:comment])
        flash[:notice] = 'Comment was successfully updated.'
        format.html { redirect_to( issue_url( @project.name, @issue.local_id ) ) }
        format.xml  { head :ok }
      else
        format.html { render :action => "edit" }
        format.xml  { render :xml => @comment.errors, :status => :unprocessable_entity }
      end
    end
  end

  # DELETE /projets/project1/issues/1/comments/1
  # DELETE /projets/project1/issues/1/comments/1.xml
  def destroy
    @comment = Comment.find(params[:id])
    @comment.destroy

    respond_to do |format|
      format.html { redirect_to(comments_url) }
      format.xml  { head :ok }
    end
  end

private

  def common
    @project = Project.find_by_name_or_id( params[:project_id] )
    @issue = Issue.find_project_local( @project, params[:issue_id] )
  end

end
